package org.dh2580.spring.batch.demo.job;

import java.util.List;

import org.dh2580.spring.batch.demo.dao.TestDao;
import org.dh2580.spring.batch.demo.dao.data.Test;
import org.dh2580.spring.batch.demo.dao.gen.TestMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * 示例Job
 *
 * @Author: hao.deng
 * @Date: 2019/5/9-下午2:40
 */
@EnableBatchProcessing
@Configuration("TestJob")
@PropertySource("classpath:conf.properties")
public class TestJob {
    private static Logger logger = LoggerFactory.getLogger(TestJob.class);

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private TestMapper testMapper;

    @Autowired
    private TestDao testDao;

    @Bean
    public Job testJob() {
        return jobBuilderFactory.get("testJob")
                .start(defaultStep())
                .build();
    }

    @Bean
    public Step defaultStep() {
        return stepBuilderFactory.get("testStep")
                .<Test, Test>chunk(1)
                .reader(new MyItemReader<>()).faultTolerant().skip(Exception.class).skipLimit(Integer.MAX_VALUE)
                .processor(new MyItemProcessor<>()).faultTolerant().skip(Exception.class).skipLimit(Integer.MAX_VALUE)
                .writer(new MyItemWriter<>()).faultTolerant().skip(Exception.class).skipLimit(Integer.MAX_VALUE)
                .build();
    }

    //-------Inner Class

    public class MyItemReader<Test> implements ItemReader<Test> {
        @Override
        public Test read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
            return null;
        }
    }

    public class MyItemProcessor<Test> implements ItemProcessor<Test, Test> {
        @Override
        public Test process(Test test) throws Exception {
            return null;
        }
    }

    public class MyItemWriter<Test> implements ItemWriter<Test> {
        @Override
        public void write(List<? extends Test> list) throws Exception {

        }
    }


}
